home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-05 | 8.0 KB | 356 lines | [TEXT/MMCC] |
- //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
- //
- // Menu.c
- //
- // Handles menu stuff.
- //
- // History:
- //
- // 950305 jb: Written
- //
- //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
-
-
- // __#Defines________________________________________________________________________
- #define kAmalgamMBARID 128
- #define kNumMenus 3
-
- // __#Headers________________________________________________________________________
- #include "Amalgam.h"
- #include "Menu.h"
-
- // __#Protos_________________________________________________________________________
- // __ Macros_________________________________________________________________________
- // __ Enums__________________________________________________________________________
- typedef enum {
- eAppleResID = 400,
- eFileResID,
- eEditResID
- } etMenuResIDs;
-
- typedef enum {
- eAppleMenuNum = 0,
- eFileMenuNum,
- eEditMenuNum
- } etMenuIndices;
-
- typedef enum {
- eQuit = 1
- } etFileItems;
-
- // __ Typedefs_______________________________________________________________________
- // __ Static Protos__________________________________________________________________
- // __ Extern Globals_________________________________________________________________
- Boolean gUserWantsMenuBar = TRUE; //used to restore menubar state
- //on a resume
-
- // __ Static Globals_________________________________________________________________
- static MenuHandle menuHdlArray[kNumMenus]; //array of our menus
-
- static Boolean menuBarHidden; //current state of menubar
- static Rect menuBarRect;
- static short menuBarHeight;
- static RgnHandle savedGrayRgn;
- static RgnHandle newGrayRgn;
- static RgnHandle coveredRgn;
-
- // __ Functions______________________________________________________________________
-
-
-
- //____ InitOurMenubar __________________________________________________________________________
- //
- // Description
- //
- // Returns void
- //
- void InitOurMenubar( void )
- {
- Handle menuBarhdl;
- short x;
-
- menuBarhdl = GetNewMBar( kAmalgamMBARID );
- SetMenuBar(menuBarhdl);
-
- for (x=0; x < kNumMenus; x++)
- menuHdlArray[x] = GetMHandle(eAppleResID + x);
-
- AddResMenu(menuHdlArray[eAppleMenuNum], 'DRVR'); //Add Apple Menu items
-
- EnableItem(menuHdlArray[eAppleMenuNum], eQuit); //well, this is ALWAYS enabled…
-
- DrawMenuBar();
-
- gUserWantsMenuBar = TRUE;
- menuBarHidden = FALSE;
-
- }//InitOurMenubar
-
-
-
-
- //____ HandleMenuChoice __________________________________________________________________________
- //
- // Our menus are pretty simple, so this doesn't have to do too much.
- //
- // Returns void
- //
- void HandleMenuChoice( long menuChoice )
- {
- short theMenu;
- short theItem;
- Str255 aStr;
-
- if (0 != menuChoice)
- {
- theMenu = HiWord(menuChoice);
- theItem = LoWord(menuChoice);
- HiliteMenu(theMenu);
-
- switch(theMenu)
- {
- case eAppleResID:
- // we don't have an About, so bleep!
- if (theItem < 2)
- {
- SysBeep(1);
- return;
- }
- // handle Apple Menu items
- GetItem(menuHdlArray[eAppleMenuNum], theItem, aStr);
- OpenDeskAcc(aStr);
- break;
- case eFileResID:
- if (eQuit == theItem)
- {
- gQuitting = TRUE; //tell the world the user wants to quit
- }
- break;
- case eEditResID:
- break;
- default:
- break;
- }
-
- HiliteMenu(0);
- CheckMenuHide(NULL); //re-hide menubar if it was shown
- }
- } // HandleMenuChoice
-
-
-
-
- //____ ToggleMenuBar __________________________________________________________________________
- //
- // Description
- //
- // Returns void
- //
- void ToggleMenuBar( Boolean showIt )
- {
- //handle redundancies
- if (showIt && !menuBarHidden)
- return;
-
- if (!showIt && menuBarHidden)
- return;
-
- //user is actually changing state of menubar.
- if (showIt)
- {
- gUserWantsMenuBar = TRUE;
- ShowMenuBar();
- }
- else
- {
- gUserWantsMenuBar = FALSE;
- HideMenuBar();
- }
-
- }//ToggleMenuBar
-
-
- #pragma mark -
-
-
- //____ StartupHideMenuBar __________________________________________________________________________
- //
- // Call this very early in your startup cycle; it saves off important information
- // about the menubar.
- //
- void StartupHideMenuBar( void )
- {
- GDHandle mainDev;
-
- savedGrayRgn = LMGetGrayRgn(); // Store the old gray region
- menuBarHeight = LMGetMBarHeight(); // Store the menu bar height
-
- //determine rectangle bounding the menubar so we can tell if
- //user has mouse-downed in it later
- mainDev = GetMainDevice();
- menuBarRect = (**mainDev).gdRect;
- menuBarRect.bottom = menuBarHeight;
-
- }//StartupHideMenuBar
-
-
- //____ ShutdownHideMenuBar __________________________________________________________________________
- //
- //
- //
- void ShutdownHideMenuBar( void )
- {
- LMSetGrayRgn( savedGrayRgn ); // Restore the gray region !
- }//ShutdownHideMenuBar
-
-
- //____ HideMenuBar __________________________________________________________________________
- //
- //
- //
- void HideMenuBar( void )
- {
- GDHandle mainDisplay;
- Rect mainDisplayBounds;
- RgnHandle workRgn;
- GrafPtr windowPort;
- GrafPtr oldPort;
- CGrafPtr windowCPort;
- WindowPtr frontWindow;
-
- // Make sure the menu bar is not already hidden
- if ( menuBarHidden )
- {
- return;
- }
-
- // Get the rectangle for the main display
- mainDisplay = GetMainDevice();
- mainDisplayBounds = ( *mainDisplay )->gdRect;
-
- // Calculate the new regions
- workRgn = NewRgn();
- RectRgn( workRgn, &mainDisplayBounds );
- newGrayRgn = NewRgn();
- coveredRgn = NewRgn();
- UnionRgn( workRgn, savedGrayRgn, newGrayRgn );
- DiffRgn( newGrayRgn, savedGrayRgn, coveredRgn );
- DisposeRgn( workRgn );
-
- // Set the new gray region
- LMSetGrayRgn( newGrayRgn );
-
- // Get the window manager port clip region
- GetPort( &oldPort );
- GetWMgrPort( &windowPort );
- SetPort( windowPort );
- SetClip( newGrayRgn );
-
- GetCWMgrPort( &windowCPort );
- SetPort( ( GrafPtr )windowCPort );
- SetClip( newGrayRgn );
-
- // Force an update of the desktop
- PaintOne( nil, newGrayRgn );
-
- // Update the windows
- frontWindow = FrontWindow();
- PaintOne( ( WindowRef )frontWindow, coveredRgn );
- PaintBehind( ( WindowRef )frontWindow, coveredRgn );
- CalcVis( ( WindowRef )frontWindow );
- CalcVisBehind( ( WindowRef )frontWindow, coveredRgn );
-
- // Mark the menu bar as hidden
- LMSetMBarHeight( 0 );
- menuBarHidden = true;
-
- // Restore the port
- SetPort( oldPort );
-
- }//HideMenuBar
-
-
- //____ ShowMenuBar __________________________________________________________________________
- //
- //
- //
- void ShowMenuBar( void )
- {
- WindowPtr frontWindow;
- GrafPtr windowPort;
- GrafPtr oldPort;
- CGrafPtr windowCPort;
-
- // Make sure the menu bar is hidden
- if ( !menuBarHidden )
- {
- return;
- }
-
- // Reset the menu bar height
- LMSetMBarHeight( menuBarHeight );
-
- // Restore the gray region
- LMSetGrayRgn( savedGrayRgn );
-
- // Recalculate the windows
- frontWindow = FrontWindow();
- CalcVis( ( WindowRef )frontWindow );
- CalcVisBehind( ( WindowRef )frontWindow, newGrayRgn );
-
- // Reset the window manager port's clipping
- GetPort( &oldPort );
- GetWMgrPort( &windowPort );
- SetPort( windowPort );
- SetClip( newGrayRgn );
- GetCWMgrPort( &windowCPort );
- SetPort( ( GrafPtr )windowCPort );
- SetClip( newGrayRgn );
-
- // Erase the menu region
- FillRgn( coveredRgn, &qd.white );
- SetPort( oldPort );
-
- // Redraw the menu bar
- HiliteMenu( 0 );
- DrawMenuBar();
-
- // Mark that the menu bar is not hidden
- menuBarHidden = FALSE;
- }//ShowMenuBar
-
-
-
- //____ CheckMenuHide __________________________________________________________________________
- //
- // This unhides or hides them menubar in certain situations:
- //
- // o Mousedown in region where menubar would be - we show menu bar
- // o we're no longer the Foreground application - we show menu bar
- // o Mousedown _not_ in menubar region - we may hide the menubar
- //
- //
- // Call the when there's a context switch, or there's a mouse-down;
- // Furthermore, call this after handling mouse-menu action to re-hide
- // the menu bar;
- //
- // Pass in mouseDowns in global coordinates if there was a mousedown,
- // otherwise, pass NULL
- //
- void CheckMenuHide( Point *mouseWhere )
- {
- if ((gInForeground == FALSE) || ((NULL != mouseWhere ) && (PtInRect(*mouseWhere, &menuBarRect))))
- {
- ShowMenuBar();
- }
- else
- {
- if (!gUserWantsMenuBar)
- HideMenuBar();
- else
- ShowMenuBar();
- }
- }//CheckMenuHide
-
-
-